﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Seesmic.Sdp.Extensibility;
using PTiRouZ.Net.SilverLight.Api.HelloTipi;
using Seesmic.Sdp.Utils;

namespace Seesmic.HelloTipi
{
    public class DeleteAction : BaseAction, ITimelineItemAction
    {
        private int _message_id;
        private string _Text;
        private Action<TimelineItemEventArgs> _action;

        public DeleteAction(int messageId, Action<TimelineItemEventArgs> action)
        {
            this.Text = string.Format("Supprimer ce message");
            this._message_id = messageId;
            this._action = action;
        }

        public bool CanExecute(TimelineItemContainer timelineItemContainer)
        {
            if (timelineItemContainer.TimelineItem is HelloTipiTimeLineItem)
            {
                HelloTipiTimeLineItem _timelineitem = (HelloTipiTimeLineItem)timelineItemContainer.TimelineItem;

                return ( (_timelineitem.Account != null) && ( (HelloTipiPlugin.getUserNameOnly(_timelineitem.Username) == _timelineitem.Account.Username) || (HelloTipiPlugin.getSite(_timelineitem.Site.name).role.Equals("webmaster")) ) );
            }
            else
                return false;

            //Todo si je suis admin je peut aussi
        }

        public void Execute(TimelineItemContainer timelineItemContainer)
        {
            HelloTipiTimeLineItem timelineItem = (HelloTipiTimeLineItem)timelineItemContainer.TimelineItem;

            apitxt api = new apitxt(timelineItem.Account.Username, timelineItem.Account.Password, "Seesmic Desktop 2");
            
            api.DestroyCompleted += delegate(object sender, DestroyEventArgs e)
            {
                if (this._action != null)
                {
                    SynchronizationHelper.Post(this._action, new TimelineItemEventArgs(timelineItemContainer));
                }

                HelloTipiPlugin.ShowBarNotification("Succès", "Message {0} supprimé", BarNotificationKind.Success, new TimeSpan(0, 0, 3), (e.result as destroy).id);                    
            };

            api.Destroy(this._message_id);
        }

        public DataTemplate Icon
        {
            get
            {
                return HelloTipiPlugin.PrivateActionIcon;
            }
        }

        public string Text
        {
            get
            {
                return this._Text;
            }
            set
            {
                this._Text = value;
            }
        }
    }
}

